package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.VisitDto;
import com.zzyl.entity.Visit;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.TimeCountVo;
import com.zzyl.vo.VisitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @Description ReservationServiceImpl
 * @Author apeng
 * @Date 2024-10-21
 */
@Service
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    //查询取消预约次数
    @Override
    public Integer getCountById() {
        Long id = UserThreadLocal.get();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 获取当前日期
        Date currentDate = new Date();
        // 使用指定的格式格式化当前日期
        String currentDay = dateFormat.format(currentDate);
       Integer count = reservationMapper.selectCancelledCount(id,currentDay);
        return count;
    }

    //新增预约
    @Override
    public void insert(VisitDto visitDto) {
        Visit visit = BeanUtil.toBean(visitDto, Visit.class);
        // 定义输出格式
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        String time = visitDto.getTime().format(formatter);
        //如果手机号和之前的手机号,一个人只能预约一次
        Integer count = reservationMapper.getPhone(time,visitDto.getMobile());
        if (count == 1){
            throw new BaseException(BasicEnum.TIME_ALREADY_RESERVATED_BY_PHONE);
        }
        LocalDateTime dateTime = visitDto.getTime();
        // 提取 LocalDate 部分
        LocalDate date = dateTime.toLocalDate();
        // 格式化为日期字符串
        String dateString = date.format(outputFormatter);
        //如果取消次数超过三次，就就不能再预约了
        Integer sum = reservationMapper.selectCancelledCount(UserThreadLocal.get(),dateString);
        if (sum == 3){
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }
        //填充属性
        visit.setStatus(0);
        reservationMapper.insert(visit);
    }


    //根据事件戳,查询每个时间段剩余预约的次数
    @Override
    public List<TimeCountVo> getCountByTime(Long time) {
        Date date = new Date(time);
        // 定义所需的日期时间格式
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        // 格式化日期时间
        String format = formatter.format(date);
       List<TimeCountVo> list = reservationMapper.getCountByTime(format);
        return list;
    }

//分页查询
    @Override
    public PageResponse page(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<VisitVo> page = reservationMapper.page(status);
        PageResponse pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    //取消
    @Override
    public void cancel(Long id) {
        reservationMapper.cancel(id);
    }


//    @Scheduled(cron = "0 1,31 * * * ?")
    public void executeTask(){
        Long id = UserThreadLocal.get();
        List<Visit> visitList = reservationMapper.selectVisitById(id);
//        LocalDateTime localDateTime = visit.getTime().plusHours(1);
//        if (localDateTime.isAfter(LocalDateTime.now())){
//            reservationMapper.update(id);
//        }
    }

}
